﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace G.Navigate
{
    public static class PhoneApplicationPageHelp
    {
        public static void ShowState(string text, TimeSpan time, Brush background, Action callback)
        {
            System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer { Interval = time };
            timer.Tick += (s, e) =>
            {
                popup.IsOpen = false;
                timer.Stop();
            };
            Grid g = new Grid();
            g.Width = 480;
            g.Background = background;
            var btn = new G.Controls.TextButton { Foreground = Application.Current.Resources["PhoneForegroundBrush"] as Brush, Margin = new Thickness(20, 10, 0, 10), Text = text, FontSize = 23, HorizontalContentAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top };
            if (callback != null)
                btn.Click += (s, e) =>
                {
                    callback();
                };
            g.Children.Add(btn);
            popup.Child = g;
            if (Microsoft.Phone.Shell.SystemTray.GetIsVisible(GetBasePage()))
                popup.VerticalOffset = 32;
            popup.IsOpen = true;
            timer.Start();
        }


        public static void ShowState(string text, Action callback = null)
        {
            ShowState(text, TimeSpan.FromSeconds(2), new SolidColorBrush(new Color() { A = 255, R = 0Xee, G = 0X77, B = 0X00 }), callback);
        }


        public static G.Navigate.GBasePage GetBasePage()
        {
            PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (frame.Content is G.Navigate.GBasePage)
            {
                return frame.Content as G.Navigate.GBasePage;
            }
            return null;
        }
    }
}
